# -*- coding: utf-8 -*-
"""
Created on Sun Jul  9 17:12:37 2017

@author: hasee
"""
import traceback
import re
#匹配规则
method_str="^REG$|^LOGIN$|^LOGOUT$|^CHAT$|^PING$|^USERLIST$|^ADDFRIEND$|^DELFRIEND$|^LISTFRIEND$|^REGETSESSION$"
username_str=".{0,16}$"
password_str="^.{32}$"
userid_str="(^\d{6}$)"
session_str="^.{8}$"
#version_str=str(version)
#分类业务处理函数
def method_head(data):
    re_method=data.get("method")
    result=match(method_str,re_method)
    for k in data.keys():
        if not k:
            result=False 
            break
    for v in data.values():
            if not v:
                result=False 
                break
    return result
	
def re_REG(data):
    re_method=data.get("method")
    re_username=data.get("username")
    re_password=data.get("password")
    result=match(password_str,re_password) and match(username_str,re_username) and match(method_str,re_method)
    return result
def re_LOGIN(data):
    re_method=data.get("method")
    re_userid=data.get("userid")
    re_password=data.get("password")
    result=match(password_str,re_password) and match(userid_str,re_userid) and match(method_str,re_method)
    return result
def re_LOGOUT(data):
    re_method=data.get("method")
    re_userid=data.get("userid")
    re_session=data.get("session")
    result=match(session_str,re_session) and match(userid_str,re_userid) and match(method_str,re_method)
    return result
def re_PING(data):
    re_method=data.get("method")
    re_userid=data.get("userid")
    re_session=data.get("session")
    result=match(session_str,re_session) and match(userid_str,re_userid) and match(method_str,re_method)
    return result
def re_USERLIST(data):
    re_method=data.get("method")
    re_userid=data.get("userid")
    re_session=data.get("session")
    result=match(session_str,re_session) and match(userid_str,re_userid) and match(method_str,re_method)
    return result
def re_CHAT(data):
    re_method=data.get("method")
    re_userid=data.get("userid")
    re_chat=data.get("request")
    re_chat_test=True
    if(isinstance(re_chat,list)):
        for re_user in re_chat:
            if(not re_chat_test):
                break
            else:
                re_chat_test=re_chat_test and match(userid_str,re_user)
    else:
        re_chat_test=False
    re_session=data.get("session")
    result=match(session_str,re_session) and match(userid_str,re_userid) and match(method_str,re_method) and re_chat_test
    return result
def re_ADDFRIEND(data):
    re_method=data.get("method")
    re_userid=data.get("userid")
    re_request=data.get("request")
    re_session=data.get("session")
    result=match(session_str,re_session) and match(userid_str,re_userid) and match(method_str,re_method) and match(userid_str,re_request)
    return result
def re_DELFRIEND(data):
    re_method=data.get("method")
    re_userid=data.get("userid")
    re_request=data.get("request")
    re_session=data.get("session")
    result=match(session_str,re_session) and match(userid_str,re_userid) and match(method_str,re_method) and match(userid_str,re_request)
    return result
def re_LISTFRIEND(data):
    re_method=data.get("method")
    re_userid=data.get("userid")
    re_session=data.get("session")
    result=match(session_str,re_session) and match(userid_str,re_userid) and match(method_str,re_method)
    return result
def re_REGETSESSION(data):
    re_method=data.get("method")
    re_userid=data.get("userid")
    re_session=data.get("session")
    result=match(session_str,re_session) and match(userid_str,re_userid) and match(method_str,re_method)
    return result
#基本匹配形式封装
def match(partern,test_object):
    
   try:
     if(not test_object):
         print(test_object)
         return False
     else:    
         re_partern=re.compile(partern)
         result=re_partern.match(test_object)
         if(bool(result)):
             return True
         else:
             return False
   except:
         print(traceback.print_exc())
 
#-------------测试代码-----------------------------  
#payload = {
#      "method": "REG",
#	"userid": "218465",
##      "username":"中文",
#	"password": "E10ADC3949BA59ABBE56E057F20F88dd",
#      "session":"JWby4xUT",
##      "request":"948543",
#      "request":["218462","109422","200519","828000","109091"],
##	"version": "1.0"
#}
#print(re_CHAT(payload)) 
